<template>
  <div class="app-container">
    <el-card class="box-card" shadow="hover" style="border: #ffffff">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">



        <el-form-item label="商户" prop="mchId">
          <el-select v-model="queryParams.mchId" clearable filterable placeholder="请选择商户" size="small">
            <el-option
              v-for="dept in deptList"
              :key="dept.deptId"
              :label="dept.deptName"
              :value="dept.deptId"
            >
              <span style="float: left">{{ dept.deptName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ dept.deptId }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="部门" prop="workDept">
          <el-cascader
             v-model="workDept"
             :options="deptOptions"
             :props="{ checkStrictly: true }"
             @change="handleSelectDept"
             size="small"
             clearable></el-cascader>
        </el-form-item>
        <el-form-item label="销售" prop="createBy">
            <el-select
              v-model="queryParams.createBy"
              placeholder="请选择销售人员"
              filterable
              clearable
              size="small">
              <el-option
                v-for="item in userList"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              >
              </el-option>
            </el-select>
        </el-form-item> -->

        <el-form-item>
          <el-button type="primary" size="small" @click="handleQuery">搜索</el-button>
          <el-button size="small" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>


        <el-table :width="100" v-loading="loading" :data="agentList"  border stripe>
          <el-table-column  type="index"  label="序号"  width="50"></el-table-column>
          <el-table-column label="昵称" align="center" prop="nickName"   />
          <el-table-column label="座席状态" align="center" prop="state"  >
             <template slot-scope="scope">

                      <el-tag type="success">{{scope.row.state}}</el-tag>

                  </template>
          </el-table-column>
          <el-table-column label="座席工号" align="center" prop="cno"   />
          <el-table-column label="登录状态" align="center" prop="loginStatus"   >
            <template slot-scope="scope">
                <span v-if="scope.row.loginStatus === 0">离线</span>
                <span v-else-if="scope.row.loginStatus === 1">在线</span>
                <span v-else-if="scope.row.loginStatus === 2">置忙</span>
                <span v-else-if="scope.row.loginStatus === 3">整理</span>
                <span v-else>其他</span>
            </template>
          </el-table-column>
          <el-table-column label="登录状态时长" align="center" prop="loginStatusDuration"  />
          <el-table-column label="登录时间" align="center" :formatter="callDurationFormat" prop="loginTime"   />

          <el-table-column label="操作" align="center"   >
            <template slot-scope="scope">
              <el-button
                type="text"
                size="mini"
                icon="el-icon-edit"
                @click="handleoffline(scope.row)"

              >下线</el-button>


            </template>
          </el-table-column>
        </el-table>


      </el-card>




  </div>
</template>

<style lang="scss" scoped>
.mockTitle{
  margin-top:15px;
  margin-bottom:10px;
  font-weight:bold;
  font-size:16px;
}
.readMockTitle{
  margin-bottom:10px;
  font-size:16px;
}
.mockEnd{
  margin-top:15px;
  margin-bottom:10px;

  .wt{
    font-size:60px;
    margin:5px 10px 10px 10px;
    float:left;
  }

  .desc{
    font-size:14px;
    float:left;
    padding-left: 10px;

    p{
      margin:6px;
    }
  }
}
.el-dialog__body{
  padding:15px 20px 0 20px;
}
</style>

<script>

import {getInfo} from '@/api/login'
import { getUserList } from "@/api/system/user";
import { allDept,treeselect } from "@/api/system/dept";
import { showAgentList,onLine } from "@/api/call/agent";

export default {
  name: "Agent",
  dicts: ['crm_own_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,

      // 总条数
      total: 0,
      //部门树
      workDept:[],
      //商户列表
      deptList:[],

      //销售列表
      userList:[],
      // 查询参数s
      queryParams: {
        pageNum: 1,
        pageSize: 500,
        mchId:null,
      },
      //查询销售
      userParams:{
        deptId:null,
        userId:null,
        status:'0',
        delFlag:'0'
      },
      // 表单参数
      form: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 弹出层 确定按钮 是否显示
      showSubmit:true,


      //登录用户ID
      loginUserId:0,


      //是否是销售
      isLeader:true,
      //是否是管理员
      isAdmin:false,

      agentList:[]
    };
  },
  created() {



    getInfo().then(
      res => {
        if (!res.isAdmin && !res.isLeader) {
          this.isLeader = false;
        }
        if (res.isAdmin) {
          this.isAdmin = true;
        }
        //登录用户ID赋值
        this.loginUserId = res.user.userId;
        this.getListDepts();
        //拉取部门
        allDept().then(response => {
          this.deptList = response.data
          this.initUserList(this.deptList[0].deptId)
          if(!this.isAdmin){
            this.queryParams.mchId=this.deptList[0].deptId;
          }
          this.getList();
      });
      })
  },
  methods: {



    getList() {
     showAgentList(this.queryParams).then(res =>{
        this.loading = false;
        this.agentList = res.data
     })
    },


    /** 获取部门 */
    getListDepts() {
       let deptParams = JSON.parse('{"status":"0", "del_flag": "0"}')
       treeselect(deptParams).then(response => {
         this.deptOptions = response.data;
       });

    },
     // 登录时长
    callDurationFormat(row, column) {//NOSONAR
        if(row.loginTime){
        // 将时间戳转换为 JavaScript Date 对象
        // const date = new Date(row.loginTime* 1000);
        // // 从 Date 对象中获取小时、分钟和秒数
        // const hours = date.getHours();
        // const minutes = date.getMinutes();
        // const seconds = date.getSeconds();
        // return hours + ":" + minutes + ":" + seconds;
        const date = new Date(row.loginTime *1000)
        const year = date.getFullYear()
        const month = ('0' + (date.getMonth() + 1)).slice(-2)
        const day = ('0' + date.getDate()).slice(-2)
        const hour = ('0' + date.getHours()).slice(-2)
        const minute = ('0' + date.getMinutes()).slice(-2)
        const second = ('0' + date.getSeconds()).slice(-2)
       return `${year}-${month}-${day} ${hour}:${minute}:${second}` //NOSONAR
}


      return 0;
    },
    timeToHMS(data) {
      let h = Math.floor(data / 3600) < 10 ? '0' + Math.floor(data / 3600) : Math.floor(data / 3600);
      let m = Math.floor((data / 60 % 60)) < 10 ? '0' + Math.floor((data / 60 % 60)) : Math.floor((data / 60 % 60));
      let s = Math.floor((data % 60)) < 10 ? '0' + Math.floor((data % 60)) : Math.floor((data % 60));
      return h + ':' + m + ':' + s;
    },
        /** 商户 */
    mchIdFormat(row, column) {
      for (const item of this.deptList) {
        if (item.deptId == row.mchId) {
          return item.deptName;
        }
      }
      return row.mchId;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {

      this.resetForm("queryForm");
      if(!this.isAdmin){
        this.queryParams.mchId=this.deptList[0].deptId;
      }

      this.handleQuery();
    },



    //下线
    handleoffline(row) {
        if(!this.queryParams.mchId){
            this.$modal.msgSuccess('商户ID不能为空');
            return
        }
        if(!row.cno){
            this.$modal.msgSuccess('坐席号不能为空');
            return
        }
        let param = {"mchId":this.queryParams.mchId,cno:row.cno};
       this.$modal.confirm('是否确认下线坐席号为："' + row.cno + '"的数据项？').then(function() {
        return onLine(param);
      }).then((res) => {
        this.$modal.msgSuccess(res.msg);
      }).catch(() => {
      });
    },





      /** 选中部门的Id*/
      handleSelectDept(deptId) {
        if(deptId.length==0){
          return;
        }
        this.userList=[];
        if(deptId.length>0){
          deptId = this.workDept[this.workDept.length-1]
        }
        this.userParams.deptId=deptId
        if(!this.isLeader){
          this.userParams.userId=this.loginUserId;
        }
        getUserList(this.userParams).then(response => {
          this.userList = response.data;
      });
      },
      /** 选中部门的Id*/
      initUserList(deptId) {
        this.userList=[];
        this.userParams.deptId=deptId
        if(!this.isLeader){
          this.userParams.userId=this.loginUserId;
        }
        getUserList(this.userParams).then(response => {
          this.userList = response.data;
      });
      },



  }
};
</script>
<style lang="scss" scoped>
.app-container{
  //background: #f5f7f9;
  ::v-deep .el-card__header{
    padding: 0;
    line-height: 60px;
  }
  .header_btn{
    display: inline-block;
    //float: right;
  }
  .tip {
    padding: 3px 12px;
    background-color: #ecf8ff;
    border-radius: 4px;
    border-left: 5px solid #50bfff;
    margin: 18px 0;
    font-size: 13px;
  }
}
.modal_form {
  .modal_form_item{
    width: 340px;
    display: inline-block
  }
  .modal_form_items{
    width: 800px;
  }
  ::v-deep .el-input__inner {
    font-size: 13px;
  }
}
@media screen and (max-width:1480px) {
  ::v-deep .el-dialog{
    left: 130px;
  }
}
</style>
